#include <cctype>
class Solution {
public:
    string trans(string& s, int n) {
        string res;
        int end = n - 1;
        for (int i = n-1; i >= 0; --i) {
            if (s[i] >= 'a' && s[i] <= 'z') {
                s[i] = toupper(s[i]);
            } else if (s[i] >= 'A' && s[i] <= 'Z') {
                s[i] = tolower(s[i]);
            } else {
                string tmp = s.substr(i+1, end-i);
                end = i-1;
                res += tmp + " ";
            }
            if (i == 0) {
                string tmp = s.substr(i, end-i+1);
                res += tmp;
            }
        }
        return res; 
    }
};